

class P10940_39643 {

	public static void main(String[] args) {
		
		f(500000);
		
		while (true) {
			
			int n = Integer.parseInt(readLn());
			
			if (n == 0) break;
			
			System.out.println(f(n));
			
			
			
		}
	}
	
	static int[] fAnswers = new int[500001];
	static int[] gAnswers = new int[500001];
	
//	f(1)=1
//	f(2*k)=2*f(k)
//	f(2*k+1)=2*g(k)
//
//	g(1)=1
//	g(2*k)=2*g(k)-1
//	g(2*k+1)=2*f(k+1)-1
	
	static int f(int k) {
		if (fAnswers[k] != 0) return fAnswers[k];
		
		
		if (k == 1) {
			fAnswers[k] = 1;
			return 1;
		}
		
		if (k % 2 == 0) {
			fAnswers[k] = 2 * f(k / 2 );
			return fAnswers[k];
		}
		
		fAnswers[k] = 2 * g (k / 2); 
		
		return fAnswers[k];
		
		
	}
	
	private static int g(int k) {
		if (gAnswers[k] != 0) return gAnswers[k];
		
		if (k == 1) {
			gAnswers[k] = 1;
			return 1;
		}
		
		if (k % 2 == 0) {
			gAnswers[k] = 2 * g(k / 2 ) - 1;
			return gAnswers[k];
		}
		gAnswers[k] = 2 * f((k / 2) + 1) - 1;
		return gAnswers[k];
	}

	

	
	static String readLn() {
		String newLine = System.getProperty("line.separator");
		StringBuffer buffer = new StringBuffer();
		int car = -1;
		try {
			car = System.in.read();
			while ((car > 0) && (car != newLine.charAt(0))) {
				buffer.append((char) car);
				car = System.in.read();
			}
			if (car == newLine.charAt(0))
				System.in.skip(newLine.length() - 1);
		} catch (java.io.IOException e) {
			return (null);
		}
		if ((car < 0) && (buffer.length() == 0))
			return (null);
		return (buffer.toString()).trim();
	}

	
}
